/* 
 * 	This file is part of VPN Monitor Project 7..

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package monitor.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.util.EnumMap;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;

import monitor.Monitor;
import monitor.gui.GuiControl.SettingsEnum;
import monitor.utility.Utility;

@SuppressWarnings("serial")
class Info extends JPanel implements ColorObserver{
	private JTable table = null;
	private JTableHeader header = null;
	
	Info(GuiControl control, EnumMap<SettingsEnum, String>  settings){
		control.addColorObserver(this);
		createPanel(control, settings);
	}
	
	@Override
	public void colorUpdated(SettingsEnum type, Color color) {
		if(type == SettingsEnum.COLOR_FOREGROUND)
			header.setForeground(color);
		else if(type == SettingsEnum.COLOR_BACKGROUND){
			table.setBackground(color);
			header.setBackground(color);
		}
	}
		
	private void createPanel(GuiControl control, EnumMap<SettingsEnum, String>  settings){
		this.setLayout(new BorderLayout());
		table = new JTable(new TrafficTable(control));
		table.setDefaultRenderer(Object.class, new CustomRenderer(control, settings));
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setBackground(SettingsEnum.color(settings.get(SettingsEnum.COLOR_BACKGROUND)));
		this.add(table, BorderLayout.CENTER);
				
		header = table.getTableHeader();
		header.setReorderingAllowed(false);
		header.setBackground(SettingsEnum.color(settings.get(SettingsEnum.COLOR_BACKGROUND)));
		header.setForeground(SettingsEnum.color(settings.get(SettingsEnum.COLOR_FOREGROUND)));
		this.add(header, BorderLayout.NORTH);
		
	}
	
	private class CustomRenderer extends DefaultTableCellRenderer implements TableCellRenderer, ColorObserver{
		private EnumMap<SettingsEnum, Color> colors = null;
		
		private CustomRenderer(GuiControl control, EnumMap<SettingsEnum, String>  settings) {
			control.addColorObserver(this);
			this.colors = new EnumMap<SettingsEnum, Color>(SettingsEnum.class);
			for(SettingsEnum t : SettingsEnum.changeable())
				this.colors.put(t, SettingsEnum.color(settings.get(t)));
		}

		@Override
		public Component getTableCellRendererComponent(JTable table, Object value, 
				boolean isSelected, boolean hasFocus, int row, int column) {
			
			if(value instanceof JProgressBar)
				return (JProgressBar) value;
			
			JLabel label = new JLabel(
					(value instanceof Long) ? Utility.reportTraffic((Long) value, Monitor.TRAFFIC_REPORT_LEVEL) : (String) value);
			label.setOpaque(true);
			label.setBackground(colors.get(SettingsEnum.COLOR_BACKGROUND));
			switch(column){
			case 0:	label.setForeground(colors.get(SettingsEnum.COLOR_FOREGROUND));
					break;
			case 1:	label.setForeground(colors.get(SettingsEnum.COLOR_DOWNLOAD));
					break;
			case 2:	label.setForeground(colors.get(SettingsEnum.COLOR_UPLOAD));
					break;
			case 3:	label.setForeground(colors.get(SettingsEnum.COLOR_TOTAL));
					break;
			};
			
			if(isSelected)
				label.setBorder(BorderFactory.createLineBorder(Color.WHITE));
			return label;
		}

		@Override
		public void colorUpdated(SettingsEnum type, Color color) {
			colors.put(type, color);
		}
	}
}
